package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.RedisKeys;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;




    /**
     * 测试 通过id获取用户信息
     * @param id
     * @return Json格式 UserInfo
     */
    @GetMapping("get")
    public Object get(String id){
        return userInfoService.get(id);
    }

    /**
     * 查询手机号码是否已被注册
     * @param phone 请求中传过来的手机号码
     * @return boolean
     */
    @GetMapping("checkPhone")
    public boolean checkPhone(String phone){
        UserInfo user = userInfoService.findByPhone(phone);
        return user == null;
    }
    /**
     * 获取注册验证码
     * @param phone
     */
    @GetMapping("sendVerifyCode")
    public Object sendVerifyCode(String phone){


        // 发送注册码到手机并缓存到redis中
        userInfoService.sendVerifyCode(phone);

        return JsonResult.success();

    }

    /**
     *  注册账号
     */
    @PostMapping("regist")
    public Object regist(String phone,String nickname,String password,String rpassword,String verifyCode){

        userInfoService.regist(phone,nickname,password,rpassword,verifyCode);

        return JsonResult.success();
    }

    /**
     * 用户登录
     */
    @PostMapping("login")
    public Object login(String username,String password){
        // 校验账号和密码
        UserInfo user = userInfoService.login(username,password);

        // 将token作为key,user作为value缓存到redis
        String token = userInfoRedisService.setToken(user);

        // 将token和user对象
        Map<String,Object> map = new HashMap<>();
        map.put("user",user);
        map.put("token",token);

        return JsonResult.success(map);
    }

    @RequireLogin
    @GetMapping("currentUser")
    public Object currentUser(HttpServletRequest request){

        // 从请求头中token
        String token = request.getHeader("token");

        UserInfo user = userInfoRedisService.getUserByToken(token);
        if (user == null){
            return JsonResult.noLogin();
        }
        user.setPassword("");
        return JsonResult.success(user);

    }


    @RequireLogin
    @GetMapping("strategies/favor")
    public Object getFavors(String sid,String userId){

        List<String> sidList = userInfoService.getFavors(sid, userId);
        return JsonResult.success(sidList);
    }



}
